<template>
  <div class="mod-prod-prodComm">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @search-reset="searchClear"
      @refresh-change="refreshChange"
    >
      <template slot="search">
        <el-col :md="8" :xs="24">
          <el-form-item :label="this.$i18n.t('productComm.recTime')">
            <el-date-picker
              v-model="recDateRange"
              type="datetimerange"
              :range-separator="this.$i18n.t('date.tip')"
              value-format="yyyy-MM-dd HH:mm:ss"
              :start-placeholder="this.$i18n.t('date.start')"
              :end-placeholder="this.$i18n.t('date.end')"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </template>
      <template slot="search">
        <el-col :md="8" :xs="24">
          <el-form-item :label="this.$i18n.t('productComm.replyTime')">
            <el-date-picker
              v-model="replyDateRange"
              type="datetimerange"
              :range-separator="this.$i18n.t('date.tip')"
              value-format="yyyy-MM-dd HH:mm:ss"
              :start-placeholder="this.$i18n.t('date.start')"
              :end-placeholder="this.$i18n.t('date.end')"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-edit"
          v-if="isAuth('prod:prodComm:update')"
          @click="addOrUpdateHandle(scope.row.prodCommId,true)"
        >{{$t("groups.edit")}}</el-button>

        <el-button
          type="success"
          size="small"
          icon="el-icon-view"
          @click="addOrUpdateHandle(scope.row.prodCommId,false)"
       >{{$t("live.view")}}</el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          v-if="isAuth('prod:prodComm:delete')"
          @click="deleteHandle(scope.row.prodCommId)"
        >{{$t("text.delBtn")}}</el-button>
      </template>
    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
  </div>
</template>

<script>
import { tableOption } from '@/crud/prod/prodComm'
import AddOrUpdate from './prodComm-add-or-update'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      searchForm: {},
      recDateRange: [],
      replyDateRange: [],
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/prod/prodComm/page'),
        method: 'get',
        params: this.$http.adornParams(Object.assign({
          current: page == null ? this.page.currentPage : page.currentPage,
          size: page == null ? this.page.pageSize : page.pageSize,
          'recStartTime': this.recDateRange === null ? null : this.recDateRange[0],
          'recEndTime': this.recDateRange === null ? null : this.recDateRange[1],
          'replyTimeStart': this.replyDateRange === null ? null : this.replyDateRange[0],
          'replyTimeEnd': this.replyDateRange === null ? null : this.replyDateRange[1]
        }, this.searchForm))
      }).then(({ data }) => {
        this.dataList = data.records
        this.dataList.forEach(element => {
          element.nickName = !element.user ? '' : element.user.nickName ? element.user.nickName : ''
        })
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id, isEdit) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, isEdit)
      })
    },
    deleteHandle: function (prodCommId) {
      this.$confirm(this.$i18n.t('admin.isDeleOper') + '?', this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/prod/prodComm/' + prodCommId),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    // 刷新回调用
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      // if (params.recTime) {
      //   this.searchForm.recStartTime = params.recTime[0]
      //   this.searchForm.recEndTime = params.recTime[1]
      // }
      // this.searchForm.recTime = null
      // if (params.replyTime) {
      //   this.searchForm.replyTimeStart = params.replyTime[0]
      //   this.searchForm.replyTimeEnd = params.replyTime[1]
      // }
      // this.searchForm.replyTime = null
      this.getDataList(this.page)
    },
    searchClear () {
      this.searchForm = {}
      console.log(this.searchForm)
      this.replyDateRange = []
      this.recDateRange = []
      // this.searchForm.recStartTime = undefined
      // this.searchForm.recEndTime = undefined
      // this.searchForm.replyTimeStart = undefined
      // this.searchForm.replyTimeEnd = undefined
      // this.searchForm.recTime = undefined
      // this.searchForm.replyTime = undefined
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
